package com.jyj;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @class: com.jyj.CustSaltRealm
 * @description: 自定义的加盐密码域
 * @author: jiangzengkui
 * @company: 教育家
 * @create: 2021-01-05 11:24
 */
public class CustSaltRealm  extends AuthorizingRealm {

    static Map<String,String> data=new HashMap<>();
    static {
        data.put("zhang","1c3a35fb4a9463ece3be44cd401fe33f");//加了密的密码
        data.put("dage","1");
    }

    /**
     *
     * @param authenticationToken 登录检验
     * @return  登录成功信息
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //得到登录用户账号
        String login_code=(String)authenticationToken.getPrincipal();
        //得到密码
        String pwd=getPwd(login_code);
        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(login_code,pwd,getName());
        //SimpleAuthenticationInfo必须设置加盐
        info.setCredentialsSalt(ByteSource.Util.bytes("jzk"));//必须加盐

        return info;
    }


    /**
     * 获得授权数据
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //得到登录用户账号
        String login_code=(String) principalCollection.getPrimaryPrincipal();
        //获得角色信息
        Set<String> roles=getRole(login_code);

        //获得权限信息
        Set<String> permissions=getPermission(login_code);

        //new  SimpleAuthorizationInfo,设置相关的角色和权限数据
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();

        info.setRoles(roles);
        info.setStringPermissions(permissions);
        return info;
    }




    /**
     * realm需要取个名称
     * @return
     */
    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "myRealm";
    }



    /**
     *这里虚拟了从数据里通过登录账号查询密码信息
     * @return
     */
    private String getPwd(String loginCode){
        String pwd=null;
        pwd=data.get(loginCode);
        if (pwd == null) {
            throw new UnknownAccountException("No account found for user [" + loginCode + "]");
        }

        return pwd;
    }

    /**模拟从数据库
     * 通过用户账号获得角色信息
     * @param login_code
     * @return
     */
    private Set<String> getRole(String login_code) {
        Set<String> roles=new HashSet<>();
        roles.add("role1");
        roles.add("role2");
        roles.add("role3");
        return roles;
    }

    /**
     * m模拟从数据库获得权限数据
     * @param login_code
     * @return
     */
    private Set<String> getPermission(String login_code) {
        Set<String> permission=new HashSet<>();
        permission.add("user:delete");
        permission.add("user:update");
        permission.add("user:create");
        return permission;
    }



}